<?php
    session_start();

    $con = mysql_connect("localhost","root","");
            if (!$con)
            {
                die('Could not connect: ' . mysql_error());
            }

    mysql_select_db("progin_171_13509008", $con);
            
    $userInsert=$_GET["userInsert"];
    $idComment=$_GET["id"];
    $text =$_GET["text"];
    $id_temp = $_SESSION['id_content'];

    $queryInsertComment = ("INSERT INTO comment (comment_text, comment_time, id_content) VALUES ('$text',now(),$id_temp);");
    mysql_query($queryInsertComment);
    
    // get next id_content for image naming
    global $curIDComment;
    $qCount = mysql_query("SELECT MAX(id_comment) FROM comment");
    $results = mysql_fetch_array($qCount);
    $curIDComment = $results['MAX(id_comment)'];

    $queryInsertCommentRelation = ("INSERT INTO user_comment (id_comment, user_name) VALUES ($curIDComment, '$userInsert')");
    mysql_query($queryInsertCommentRelation);
    
    // Check for achievement
    // get next id_content for image naming
    global $totalComment;
    $qCountTotalComment = mysql_query("select count(*)as totalComment from user_comment where user_name ='$userInsert'");

    while ($row1 = mysql_fetch_array($qCountTotalComment)){
        $totalComment = $row1['totalComment'];
    }
    if ($totalComment =='1'){
        //Insert Achievement ke user
        mysql_query("Insert into user_achievement (user_name, id_achievement) VALUES ('$userInsert', 3) ") or die ("GAGAL MELAKUKAN QUERY. " . mysql_error());
    }
    if ($totalComment =='20'){
        mysql_query ("Insert into user_achievement (user_name, id_achievement) VALUES ('$userInsert', 4) ") or die ("GAGAL MELAKUKAN QUERY. " . mysql_error());
    }
    echo $totalComment;



    // id Content nanti diubah pake session dari user
    $queryGetTotalComment = "select count(id_comment) as totalComment  from comment where id_content=$id_temp";
    $resultTotalComment = mysql_query($queryGetTotalComment);
    while ($rowTotal = mysql_fetch_array($resultTotalComment)){
        echo "<h3 id=\"totalComment\">Total Comment : ".$rowTotal['totalComment']."</h3>";
    }
    
    $querySelectComment = "select id_comment,user_name, comment_text, Concat ( Floor(Hour(TIMEDIFF (comment_time, now()))/24), ' days ',
                                               MOD(HOUR(TIMEDIFF(comment_time, now())), 24), ' hours ',
                                               MINUTE (TIMEDIFF(comment_time, now())), ' minutes')as time from
    comment natural join user_comment where comment.id_content =$id_temp order by time";
    $resultGetComment = mysql_query($querySelectComment);
    
    while ($rowComment = mysql_fetch_array($resultGetComment)){
        echo '<div class="comment_list">';
        echo "<img src='images/avatar/like_button.jpg' width='50px' height='50px' style='float: left;'></img>";
        echo "<button type='button' onclick='deleteComment(\"".$userInsert."\",".$rowComment['id_comment'].")' style='float: right;'>X</button>";
        echo '<h5>'.$rowComment['user_name'].'</h5><br/>';
        echo '<h5>'.$rowComment['time'].'</h5><br/>';
        echo '<h3>'.$rowComment['comment_text'].'</h3>';
        echo '</div>';
    }

?>